// 引入 createStore , 当前这玩意已经不推荐使用，而是推荐我们使用 RTK
import { createStore } from "redux";
import { INCREMENT, DECREMENT, CHANGEMSG } from "./actionTypes";

// 定义初始化 state 数据
const initalState = {
  msg: "~",
  count: 1,
};

// 定义根Reducer纯函数
function rootReducer(state = initalState, action: any) {
  switch (action.type) {
    case INCREMENT:
      return {
        ...state,
        count: state.count + 1,
      };

    case DECREMENT:
      return {
        ...state,
        count: state.count - 1,
      };

    case CHANGEMSG:
      return {
        ...state,
        msg: action.payload ? action.payload : state.msg + "~",
      };

    default:
      return state;
  }
}

// 创建 store
const store = createStore(
  rootReducer,
  window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__()
);

// 导出 store
export default store;
